var nameReg = /^[\u4e00-\u9fa5a-zA-Z0-9]{1,10}$/;
var cellReg = /^1[34578]{1}\d{9}$/;
var addressReg = /\S$/;
$(function(){
	$("#back").on("click", function(){
		$.go(-1);
	});
	
	//默认地址
	$("#addressSetDefault").on("click", function(){
		$(this).toggleClass("selected");
	});
	//保存地址
	$("#addressSave").on("click", function(){
		checkaddress();
	});
	//选择地区
	$("#areaWrap").on("click", function(){
		$.loader("加载中...");
		$("#areaPage").show().load(areaPageUrl, function(){
			initAreaSelect();
			$.loaded();
		});
	});
	//关闭地区选择
	$(document).on("click", "#hideAreaSelect", function(){
		$("#areaPage").hide();
	})
})
//校验地址表单数据
function checkaddress(){
	var error = $("#addressError");
	var name = $("#name").val();
	var cell = $("#cell").val();
	var area = $("#area").val();
	var detail = $("#detail").val();
	var isDefault = $("#addressSetDefault").is(".selected")?1:0;
	if(name==""){
		error.text("收货人不能为空");
	}else if(!name.match(nameReg)){
		error.text("请输入正确的收货人姓名");
	}else if(cell==""){
		error.text("手机号码不能为空");
	}else if(!cell.match(cellReg)){
		error.text("请输入正确的手机号码");
	}else if(area==""){
		error.text("请选择省市区");
	}else if(detail==""){
		error.text("详细地址不能为空");
	}else if(!detail.match(addressReg)){
		error.text("请输入正确的地址");
	}else{
		error.text('');
		var data = {"consignee":name, "cell":cell, "address":detail, "is_default":isDefault, "editFlag": editFlag};
		$.extend(data, JSON.parse($("#area").attr("data")));
		editFlag==1&&(data.uid=uid);
		$.doAjax({
			url: addressSave,
			type: "post",
			data: data,
			success: function(data){
				if(data.flag){
					$.tips("地址保存成功");
					$.go(-1);
				}else{
					$.tips("地址保存失败");
				}
			}
		});
	}
}
//选择地区
function initAreaSelect(){
	var areaTitle = $("#areaTitle");
	var areaList = $("#areaList");
	var province = $("#province")
	var city = $("#city");
	var district = $("#district");
	var town = $("#town");
	var provinceScroll = new IScroll('#areaSection1', {click: false, tap: true});
	var cityScroll = new IScroll('#areaSection2', {click: false, tap: true});
	var districtScroll = new IScroll('#areaSection3', {click: false, tap: true});
	getProvince();
	areaTitle.on("click", "div", function(){
		if(!$(this).text()==""){
			$(this).text("请选择").addClass("active").siblings(".active").removeClass("active");
			var activeIndex = $(this).index();
			areaTitle.children("div").each(function(i){
				if(i>activeIndex){
					$(this).text("").attr("uid","");
					areaList.children().eq(i).find("ul").html('');
				}
			});
			switchAreaList(activeIndex);
		}
	});
	areaList.on("tap", "li", function(){
		var uid = $(this).attr("uid");
		var selectedText = $(this).text();
		var activeIndex = $(this).closest(".area-section").index();
		areaTitle.children("div").eq(activeIndex).text(selectedText).attr("uid", uid).removeClass("active").next().addClass("active").text("请选择");
		activeIndex++;
		if(activeIndex==1){
			getCity(uid, activeIndex);
		}else if(activeIndex==2){
			getDistrict(uid, activeIndex);
		}else{
			pickAreaData();
			return false;
		}
	});
	function pickAreaData(){
		var json = {};
		var arr = [];
		areaTitle.children("div").each(function(i){
			json["s"+(i+1)+"_uid"] = $(this).attr("uid");
			arr.push($(this).text());
		});
		$("#area").attr("data",JSON.stringify(json)).val(arr.join("-"));
		$("#areaPage").hide();
	}
	function switchAreaList(i){
		var moveX = i*$(window).width();
		areaList.css({"-webkit-transform":"translateX(-"+moveX+"px)", "transform":"translateX(-"+moveX+"px)"});
	}
	function getProvince(){
		getArea(1, 1, function(data){
			province.html(data.data);
			provinceScroll.refresh();
		});
	}
	function getCity(id, index){
		getArea(id, 2, function(data){
			switchAreaList(index);
			city.html(data.data);
			cityScroll.refresh();
		});
	}
	function getDistrict(id, index){
		getArea(id, 3, function(data){
			switchAreaList(index);
			district.html(data.data);
			districtScroll.refresh();
		});
	}
	function getArea(parentId, layer, callback){
		$.loader("加载中...");
		$.doAjax({
			url: areaUrl,
			data: {parent_id: parentId, layer: layer},
			success: function(data){
				if(data.flag){
					if(data.data==""){
						pickAreaData()
					}else{
						$.isFunction(callback)&&callback(data)
					}
					
				}else{
					$.tips("地址加载失败");
					return false;
				}
				$.loaded();
			}
		});
	}
}
